import { getLoader } from '../utils'
import { RollupTypescriptOptions } from '@rollup/plugin-typescript'
import { resolve } from 'path'

export const typescriptPlugin = function (declarationDir?: string, tsconfigPath?: string) {
    const declarationObj: any = {
        declaration: false,
        emitDeclarationOnly: false
    }
    if (declarationDir) {
        declarationObj.declaration = true
        declarationObj.emitDeclarationOnly = true
        declarationObj.outDir = declarationDir
    }
    const typescript = getLoader<RollupTypescriptOptions>('@rollup/plugin-typescript')
    return typescript({
        tsconfig: tsconfigPath || resolve('.', 'tsconfig.json'),
        compilerOptions: {
            ...declarationObj
        },
        exclude: ['node_modules']
    })
}
